package com.wd.jetpack.bean

import java.net.HttpURLConnection
import java.net.URL

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V5.9.3, 2021/6/3]
 * @since V5.9.3
 */
sealed class Result<out R> {
    data class Success<out T>(val data: T) : Result<T>()
    data class Error(val exception: Exception) : Result<Nothing>()
}

class LoginRepository(private val responseParser: LoginRepository) {
    companion object {
        private const val loginUrl = "https://example.com/login"
    }

    fun makeLoginRequest(jsonBody: String): Result<LoginRepository> {
        val url = URL(loginUrl)
        (url.openConnection()as?HttpURLConnection)?.run {
            requestMethod = "POST"
            setRequestProperty("Content-Type","application/json; utf-8")
            setRequestProperty("Accept","application/json")
            doOutput = true
            outputStream.write(jsonBody.toByteArray())
            return Result.Success(responseParser)
        }
        return Result.Error(Exception("Cannot open HttpURLConnection"))
    }
}